<?php
/**
 *
 *
 *
 */
namespace Yz\modules\Uploads;

class Model_Upload
    extends \Yz\modules\Generic\Model_Default
{
    const DEFAULT_ITEM   = 'UploadedFile';

    const UPLOAD_ACTION = 'Upload';
    const UPLOAD_CONTROLLER = 'Upload';
    const UPLOAD_PATH   = 'www/uploads';

    protected $uploadPath;

    /**************************
     *      Public methods    *
     **************************/

    /**
     *
     *  @return Item
     *  @throw
     */
    public function processUpload($uploadName)
    {
        $action = $this
            ->selectAction(static::UPLOAD_ACTION)
            ->setUploadName($uploadName)
        ;
        return $this->_process($action);
    }


    public function getUploadPath()
    {
        $uploadPath =& $this->uploadPath;
        if (null === $uploadPath) {
            $uploadPath = static::UPLOAD_PATH;
        }
        return $uploadPath;
    }

    /*****************************
     *      Protected methods    *
     *****************************/

    protected function _process($action)
    {
        $uploadPath = $this->getUploadPath();
        $values = $action
            ->setUploadPath($uploadPath)
            ->execute()
            ->fetch()
        ;
        $item = $this->selectItem();
        return $item->assignValues($values);
    }

}
